/**
 * 
 */
package ask.speedy;

import java.io.File;
import java.util.Map;

import speedy.ask.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import ask.speedy.basic.BasicNode;
import ask.speedy.util.Constant;
import ask.speedy.util.ContainerParser;

/**
 * @author passa
 *
 */
public class QuestionActivity extends Activity {
	
	private BasicNode basicNode;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // (NEW)
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW)
        
        this.basicNode = ContainerParser.tree;
        
        setContentView(R.layout.question);
        setLayoutQuestion();
    }
    
    
    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	private void setLayoutQuestion() {
        
		final Map<String, BasicNode> childMap = basicNode.getChilds();
		
		//set radio group content
		final RadioGroup rg = (RadioGroup)findViewById(R.id.radiobuttons);
		rg.removeAllViews();
		int index = 0;
		for (String key : childMap.keySet()) {
			RadioButton rb = new RadioButton(QuestionActivity.this);
			rb.setTextColor(Color.DKGRAY);
		    rb.setText(key);
		    rg.addView(rb, index++);
		}
		
		//set question
		TextView tquestion = (TextView)findViewById(R.id.question_area);
		tquestion.setText(basicNode.getInformation());
		tquestion.setTextColor(Color.DKGRAY);
		
		ImageView imageQuestion = (ImageView)findViewById(R.id.imageViewQuestion);
		
		//set image
		if (!basicNode.getUrlImage().equals("")) {
			imageQuestion.setVisibility(ImageView.VISIBLE);
			
			File imgFile = new  File(Constant.LOCAL_IMAGE_PATH + basicNode.getUrlImage());
			if(imgFile.exists()){
			    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
				imageQuestion.setImageBitmap(myBitmap);
			}
		} else {
			imageQuestion.setVisibility(ImageView.GONE);
		}
		
		//set prev button
		Button prevButton = (Button)findViewById(R.id.prevButton);
		if (basicNode.getParent() != null) {
			prevButton.setVisibility(Button.VISIBLE);
			prevButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					basicNode = basicNode.getParent();
					setLayoutQuestion();
				}
			});
		} else {
			prevButton.setVisibility(Button.GONE);
		}
		
		//set next button
		Button nextButton = (Button)findViewById(R.id.nextButton);
		if (childMap.size() != 0) {
			nextButton.setVisibility(Button.VISIBLE);
			
			nextButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.next));
			nextButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int selectedIdx = rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));
					if (selectedIdx != -1) {

						RadioButton radio = (RadioButton) rg.getChildAt(selectedIdx);
						basicNode = childMap.get(radio.getText().toString());
						
						setLayoutQuestion();
					}
				}
			});
		} else {
			nextButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.selesai));
			nextButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			});
		}
	}
}
